<?php

class RedeemrecordController extends UserLoginBaseC
{
    public function init()
    {
        parent::init();
        if(yii::app()->user->kind == 0)
        {
            $this->error('亲，您的账户类型是“商家用户”，不能进行积分商品兑换！');
        }
    }    
	public function actionEditOrder()
	{
/*        $mixResult = array('address');
        $this->error(json_encode($mixResult));*/
        $id = Yii::app()->request->getParam('id');
        $objModel = RedeemRecordsModel::model()->findByPk($id);

        if(!$objModel)
        {
            $this->error('找不到记录');
        }

        if(!$mixResult = $objModel->canEditOrder())
        {
        	$this->error($objModel->getOneError());
        }else if(is_array($mixResult))
        {
            echo json_encode(array('status' => -1, 'info' => '信息不完整', ''));
            exit;
        }


        if($order = Yii::app()->request->getParam('order_sn'))
        {
        	$objModel->order_sn = $order;
        	$objModel->order_submit_time = time();


            if(!$objModel->save(true, array('order_sn', 'order_submit_time'))){
                $this->error($objModel->getOneError());
            }
        	

            //获取卖家的信息 
            $objMember = MembersModel::model()->findByPk($objModel->RedeemModel->uid);
            if(!empty($objMember->email))
            {
                $mail = Yii::createComponent('application.extensions.mailer.EMailer');
                $content = "用户：{$objModel->username},购买的积分商品：{$objModel->RedeemModel->title},已提交了订单,商品ID：{$objModel->RedeemModel->product_id},订单号：{$objModel->order_sn}，请尽快确认，超过三天将自动确认通过";
                $mail->sendEmail($objMember->email, '用户积分商品订单提交', $content);
            }
            if(!empty($objMember->phone))
            {
                $moblie = $objMember->phone;
                $content = "用户：{$objModel->username},购买的积分商品：{$objModel->RedeemModel->title},已提交了订单,商品ID：{$objModel->RedeemModel->product_id},订单号：{$objModel->order_sn}，请尽快确认，超过三天将自动确认通过";
                Sns::getInstance()->send($moblie, $content);                
            }
            //TODO 异步通知商家,买家更新了订单


            //发短信给商家


       
        	$this->success('修改成功');
        }else
        {
        	$this->error('提交的订单不能为空');
        }
	}
	public function actionIndex()
	{
        $objModel = new RedeemRecordsModel('search');

        $objModel->resetAttributes();


        if( isset( $_GET['RedeemRecordsModel'] ) )
        {
            $objModel->attributes = $_GET['RedeemRecordsModel'];
        }
        $objModel->uid = yii::app()->user->id;

        $objCriteria = $objModel->createSearchCriteria(); 

        $objPager = new CPagination($objModel->count($objCriteria));
        $objPager->pageSize = 20;
        $objPager->applyLimit($objCriteria);

        $objSort = new CSort('RedeemRecordsModel');
        $objSort->applyOrder($objCriteria);

        $objModels = $objModel->with('RedeemModel')->findAll($objCriteria);
        $this->assign('pages', $objPager);
        $this->assign('sort', $objSort);
        $this->assign('model', $objModel); 
        $this->assign('models', $objModels);
        $this->render('index');   		
	}
	public function actionView()
	{
        $id = Yii::app()->request->getParam('id');
        $objModel = RedeemRecordsModel::model()->findByPk($id);
        if(!$objModel)
        {
            $this->error('找不到记录', self::U($url));
        }
        $this->assign('model', $objModel); 
        $this->render('view');  		
	}

    public function actionExchange()
    {
        $id = Yii::app()->request->getParam('id');

        $objModel = RedeemModel::model()->findByPk($id);

        if(!$objModel)
        {
            $this->error('找不到此记录');
        }

        if(is_array($arrResult = $objModel->doExchange()))
        {
            $this->error($arrResult['error'].'<a target="_blank" style="color:blue;" href="http://www.1zw.com/article/12.html">点击了解如何获取更多的积分</a>', isset($arrResult['url']) ? $arrResult['url'] : '');
        }else if($arrResult == true)
        {
            $this->success('商品兑换成功，请购买好评后，提交订单，然后耐心等待工作人员的确认及邮寄商品',$objModel->url);
        }else
        {
            $this->error($objModel->getOneError());
        }
    }

    public function actionForm()
    {
        $this->layout = false;

        $MembersAddress = MembersAddressModel::model()->getMemberAddres(Yii::app()->user->id);
        if(empty($MembersAddress))
        {
            $MembersAddress = new MembersAddressModel();
        }

        //旺旺，支付宝
        $memberTaobao = MembersTaobaoModel::model()->getMemberTB(Yii::app()->user->uid);


        $memberInfo = MembersModel::model()->findByPk(Yii::app()->user->uid);

        if(isset($_POST['MembersAddressModel']))
        {
            if(!$memberTaobao)
            {

                $MembersTaobaoModel = new MembersTaobaoModel();
                $MembersTaobaoModel->taobao_user_nick = $_POST['taobao_user_nick'];
                $MembersTaobaoModel->uid = Yii::app()->user->id;
                $MembersTaobaoModel->add_time = time();
                if ($MembersTaobaoModel->validate()) 
                {
                    $MembersTaobaoModel->save();
                }else
                {
                    $this->error($MembersTaobaoModel->getOneError());
                }                            
            }

            $MembersAddress->attributes = $_POST['MembersAddressModel'];
            if(empty($MembersAddress->address))
            {
                $this->error('地址不能为空');
            }

            if(empty($MembersAddress->consignee))
            {
                $this->error('联系人不能为空');
            }

            if(empty($MembersAddress->mobile))
            {
                $this->error('手机号不能为空');
            }

            if(empty($MembersAddress->zipcode))
            {
                $this->error('邮编不能为空');
            }

            if(empty($MembersAddress->uid))
            {
                $MembersAddress->setIsNewRecord(true);
                $MembersAddress->uid = Yii::app()->user->id;                
            }


            //$memberInfo->alipay = $_POST['MembersModel']['alipay'];
            //$memberInfo->alipay_name = $_POST['MembersModel']['alipay_name'];
            $memberInfo->attributes = $_POST['MembersModel'];

            if(empty($memberInfo->alipay))
            {
                $this->error('支付宝账号不能为空');
            }

            if(empty($memberInfo->alipay_name))
            {
                $this->error('支付宝姓名不能为空');
            }

            if($MembersAddress->validate())
            {
                $MembersAddress->save();
            }else
            {
                $this->error($MembersAddress->getOneError());
            }

            if($memberInfo->validate())
            {
                $memberInfo->save(false, array('alipay', 'alipay_name'));
                $this->success('OK');
            }else
            {
                $this->error($memberInfo->getOneError());
            }

        }



        $this->render('_form', array('memberAddress' => $MembersAddress, 'memberTaobao' => $memberTaobao, 'memberInfo' => $memberInfo));          
          
    }
    //积分晒单
    public function actionSunsingle(){
        $id = Yii::app()->request->getParam('id');
        $experience = Yii::app()->request->getParam('experience');
        if(!$experience){
            $this->error('心得内容不能为空');
        }
        $objModel = RedeemRecordsModel::model()->findByPk($id);
        $uid = yii::app()->user->id;
        if(!$objModel || $objModel->uid != $uid)
        {
            $this->error('找不到记录');
        }
        if($objModel->is_experience > 0){
            $this->error('此积分记录已经晒过单');
        }
        yii::import('ext.yzwAPI.*');
        if(!DzAPI::getInstance()->canSunsingle()){
            $this->error('请先到论坛激活账号');
        }
        if($objModel->post($experience)){
            $this->success('晒单成功');
        }else{
            $this->error('晒单失败');
        }       
    }

}